package com.maxuxu.scihome.config;

import com.maxuxu.scihome.interceptor.UserRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

/**
 * @className: ShiroConfig
 * @author: Maxuxu
 * @date: 2022/7/7 15:36
 * @description: shiro安全框架的配置文件
 */
@Configuration
public class ShiroConfig {
    // 步骤三：ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(defaultWebSecurityManager);
        LinkedHashMap<String, String> filtermap = new LinkedHashMap<>();
        filtermap.put("/js/**", "anon");
        filtermap.put("/css/**", "anon");
        filtermap.put("/images/**", "anon");
        filtermap.put("/user/ajaxlogin", "anon");
        filtermap.put("/tologin", "anon");
        filtermap.put("/user/logout", "anon");


        filtermap.put("/user/showAddUser.html","perms[user:admin]");
        filtermap.put("/user/showUpdateUser.html","perms[user:admin]");
        filtermap.put("/user/removeUser", "perms[user:admin]");
        filtermap.put("/notice/showAddNotice.html","perms[user:admin]");
        filtermap.put("/notice/showUpdateNotice.html","perms[user:admin]");
        filtermap.put("/notice/removeNotice", "perms[user:admin]");
        filtermap.put("/notice/*", "authc");
        filtermap.put("/job/showAddJob.html","perms[user:admin]");
        filtermap.put("/job/showUpdateJob.html","perms[user:admin]");
        filtermap.put("/job/removeJob", "perms[user:admin]");
        filtermap.put("/job/*", "authc");
        filtermap.put("/employee/showAddEmployee.html","perms[user:admin]");
        filtermap.put("/employee/showUpdateEmployee.html","perms[user:admin]");
        filtermap.put("/employee/removeEmployee", "perms[user:admin]");
        filtermap.put("/employee/*", "authc");
        filtermap.put("/document/showAddDocument.html","perms[user:admin]");
        filtermap.put("/document/showUpdateDocument.html","perms[user:admin]");
        filtermap.put("/document/removeDocument", "perms[user:admin]");
        filtermap.put("/document/*", "authc");
        filtermap.put("/dept/showAddDept.html","perms[user:admin]");
        filtermap.put("/dept/showUpdateDept.html","perms[user:admin]");
        filtermap.put("/dept/removeDept", "perms[user:admin]");
        filtermap.put("/dept/*", "authc");
        filtermap.put("/sendEmail","perms[user:admin]");

        filtermap.put("/main", "authc");
        filtermap.put("/main.html", "authc");
        bean.setUnauthorizedUrl("/403");
        bean.setLoginUrl("/tologin");
        bean.setSuccessUrl("/main");
        filtermap.put("/user/**", "authc");

        bean.setFilterChainDefinitionMap(filtermap);

        return bean;
    }
    //步骤二： DefaultWebSecurityManager
    @Bean(name="securityManager")
    public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 关联userrealm
        securityManager.setRealm(userRealm);
        return securityManager;
    }
    //步骤一 前面创建的自定义类
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }



    @Bean
    public DefaultWebSessionManager sessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        return sessionManager;
    }
}
